/***
This figure compares the industry composition of spending in MARTS, Affinity and Womply. 
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root 
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"

* Create required subfolders
cap mkdir "${root}/results/Spending"

*-------------------------------------------------------------------------------
* Step 1: Clean MRTS data
*-------------------------------------------------------------------------------

use "${root}/data/derived/MARTS/MARTS monthly sales by industry.dta", clear

keep if month == ym(2020,1) & is_adj == 0 & (inlist(cat_code, "441", "445", "454", "722", "452", "447") | inlist(cat_code, "446", "444", "448", "453", "442", "443", "451"))

keep sales cat_code
rename (sales cat_code) (mrts naicscode)

tempfile MRTS
save `MRTS', replace

*-------------------------------------------------------------------------------
* Step 2: Clean Affinity Data
*-------------------------------------------------------------------------------
project, uses("${root}/data/derived/Affinity/national_day_mcc.dta")
use "${root}/data/derived/Affinity/national_day_mcc.dta", clear

* check file structure
isid date mcc 

rename mcc mcc_numeric
project, uses("${root}/data/dvc/Crosswalks/mcc_naics_cw.dta")
merge m:1 mcc_numeric using "${root}/data/dvc/Crosswalks/mcc_naics_cw.dta", nogen keepusing(NAICS*)
drop if mi(NAICS_id) | mi(date)

* collapse to date x NAICS_id 
gcollapse (sum) total_spend, by(date NAICS_id)

* check file structure 
isid date NAICS_id

* Only keep industries we can match to MRTS
gen naicscode = substr(NAICS_id,1,3)

* collapse to 2-digit NAICS codes in Jan 2020
keep if year(date) == 2020 & month(date) == 1
gcollapse (sum) total_spend, by(naicscode)

rename (total*) (affinity*)
		
tempfile affinity
save `affinity'

*-------------------------------------------------------------------------------
* Step 3: Clean Womply Data
*-------------------------------------------------------------------------------
project, uses("${root}/data/dvc/Industry Shares of Consumer Spending and Business Revenues/womply subcategory x monthly 2020.dta")
use "${root}/data/dvc/Industry Shares of Consumer Spending and Business Revenues/womply subcategory x monthly 2020.dta", clear

* merge onto subcategory NAICS 2-digit and 3-digit
project, uses("${root}/data/dvc/Crosswalks/crosswalk_naics_womply_subcategories_3_digit.dta")
merge m:1 womplysubcategory using "${root}/data/dvc/Crosswalks/crosswalk_naics_womply_subcategories_3_digit.dta", nogen 
keep if month == 1
replace naics_3 = "44-" if naics_2 == "44-45" & naics_3 == ""
drop if mi(naics_3)

gcollapse (sum) total*, by(naics_3)
rename naics* naicscode
rename (total*) (womply*)

*-------------------------------------------------------------------------------
* Step 4: Merge and Plot
*-------------------------------------------------------------------------------

* merge affinity 
merge 1:1 naicscode using `affinity', nogen 

* merge MRTS 
merge 1:1 naicscode using `MRTS', nogen
rename naicscode naics 

gcollapse (sum) womply_net_revenue affinity_spend mrts, by(naics)

* keep only 44-45, 722 
keep if naics == "722" | substr(naics,1,2) == "44" | substr(naics,1,2) == "45" 

* generate shares out of QSS services
foreach var in womply_net_revenue affinity_spend mrts {
	gegen `var'_total = total(`var')
	gen `var'_share = 100*(`var' / `var'_total)
	
	* Check that the unclassified share is very small 
	sum `var'_share if naics == "44-"
	assert r(mean) < 1e-2 if r(N) > 0
} 

*Shorten descriptions 
gen short = ""
replace short = "Motor Vehicles" if naics == "441"
replace short = "Furniture" if naics == "442"
replace short = "Electronics" if naics == "443"
replace short = "Building Material" if naics == "444"
replace short = "Food & Beverage" if naics == "445"
replace short = "Health & Personal Care" if naics == "446"
replace short = "Gas Stations" if naics == "447"
replace short = "Clothing" if naics == "448"
replace short = "Sporting & Hobby" if naics == "451"
replace short = "General Merchandise" if naics == "452"
replace short = "Miscellaneous" if naics == "453"
replace short = "Nonstore Retailers" if naics == "454"
replace short = "Food Service" if naics == "722"

graph hbar mrts_share affinity_spend_share womply_net_revenue_share , ///
	over(short, sort(mrts_share) descending label(ticks labsize(small)) gap(*3)) ///
	legend(order(1 "MARTS" 2 "Affinity" 3 "Womply") ring(0) pos(5) col(1) size(2.5)) ///
	ytitle("Percent of Total Retail and Food Service Revenue in January 2020 (%)", size(2)) ///
	ylabel(0 "0%" 10 "10%" 20 "20%" 30 "30%", nogrid labsize(small)) ///
	ysize(8) /// 
	${title_`version'} 
oi_graph_export "${root}/results/Spending/MRTS, Affinity, Womply Spending Mix", type(${fig_type})
